package com.gin.kotlin.no_gay_alliance.fragment.replyhistory

import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.annotation.StringRes
import androidx.core.view.MenuProvider
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.replyhistory.ReplyHistoryConfigActivity
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingFragment
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.databinding.FragmentReplyHistoryConfigBinding

abstract class AbstractReplyHistoryConfigFragment : AbstractBindingFragment<FragmentReplyHistoryConfigBinding>(), MenuProvider {

    lateinit var autoRefresh: Runnable

    override fun onResume() {
        super.onResume()
        (requireActivity() as? ReplyHistoryConfigActivity)?.also { it.currentFragment = this }
    }

    override fun onCreateView() {
        val b = this.b ?: return
        b.recyclerView.adapter = obtainAdapter()
        b.swipeRefreshLayout.setOnRefreshListener { query() }
        this.autoRefresh = Runnable { query() }
        query()
    }

    override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        when (menuItem.itemId) {
            R.id.mi_info -> AlertDialogUtils.builder(
                requireContext(),
                getString(R.string.dialog_title_function_info),
                getString(obtainFunctionDescription())
            ).show()

            R.id.mi_add -> showAddDialog()
        }
        return true
    }

    @StringRes
    abstract fun obtainFunctionDescription(): Int

    override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
    }

    /**
     * 打开弹窗，添加新实体
     */
    abstract fun showAddDialog()

    /**
     * 查询数据并显示
     */
    abstract fun query()

    /**
     * 获取适配器
     * @return AbstractBindingRecyclerViewAdapter<*,*>
     */
    abstract fun obtainAdapter(): AbstractBindingRecyclerViewAdapter<*, *>
}